#
# Copyright (C) [2024] Xingyun Integrated Circuit, Inc.
#
# GreenCode was a private technology asset of Xingyun Integrated Circuit， Inc （Confidential）
# Author: Shawn.Tan
# Date: 2025.10.28
#
# History: Initial Version 2025.10.28
#
#
from VectorTestSequence import VectorTestSequence
from base.ChoicesModifier import ChoicesModifier
from gpgpu.EnvGPGPU import EnvGPGPU
from gpgpu.GenThreadGPGPU import GenThreadGPGPU


#  This test verifies that vector sign and zero extension instructions are
#  generated legally.
class MainSequence(VectorTestSequence):
    def __init__(self, aGenThread, aName=None):
        super().__init__(aGenThread, aName)

        self._mInstrList = (
            "VSEXT.VF2##CALM",
            "VSEXT.VF4##CALM",
            "VSEXT.VF8##CALM",
            "VZEXT.VF2##CALM",
            "VZEXT.VF4##CALM",
            "VZEXT.VF8##CALM",
        )

    # Set up the environment prior to generating the test instructions.
    def _setUpTest(self):
        choices_mod = ChoicesModifier(self.genThread)

        choice_weights = {
            "0x0": 0,
            "0x1": 0,
            "0x2": 0,
            "0x3": 10,
            "0x4": 0,
            "0x5": 0,
            "0x6": 0,
            "0x7": 0,
        }
        choices_mod.modifyRegisterFieldValueChoices("vtype.VSEW", choice_weights)

        choices_mod.commitSet()

    # Return the maximum number of test instructions to generate.
    def _getMaxInstructionCount(self):
        return 1000

    # Return a list of test instructions to randomly choose from.
    def _getInstructionList(self):
        return self._mInstrList

    # Verify additional aspects of the instruction generation and execution.
    #
    #  @param aInstr The name of the instruction.
    #  @param aInstrRecord A record of the generated instruction.
    def _performAdditionalVerification(self, aInstr, aInstrRecord):
        eew_denominator = int(aInstr[8])
        vd_val = aInstrRecord["Dests"]["vd"]
        vs2_val = aInstrRecord["Srcs"]["vs2"]
        self.assertNoRegisterOverlap(
            aInstr, vd_val, vs2_val, aRegCountMultipleB=(1 / eew_denominator)
        )

        vm_val = aInstrRecord["Imms"]["vm"]
        if (vm_val == 0) and (vd_val == 0):
            self.error("Instruction %s is masked with v0 as the destination register" % aInstr)


MainSequenceClass = MainSequence
GenThreadClass = GenThreadGPGPU
EnvClass = EnvGPGPU
